package com.lmx.smsmonitor.view;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.lmx.smsmonitor.R;

import org.apache.commons.lang3.StringUtils;

public class EmailAutoCompleteTextView
        extends androidx.appcompat.widget.AppCompatAutoCompleteTextView
        implements View.OnFocusChangeListener {

    private static final String[] emailSuffix = {
            "@qq.com", "@163.com", "@126.com", "@gmail.com", "@sina.com", "@hotmail.com",
            "@yahoo.cn", "@sohu.com", "@foxmail.com", "@139.com", "@yeah.net", "@vip.qq.com", "@vip.sina.com"
    };

    public EmailAutoCompleteTextView(Context context) {
        this(context, null);
        init(context);
    }

    public EmailAutoCompleteTextView(Context context, AttributeSet attrs) {
        this(context, attrs, R.attr.autoCompleteTextViewStyle);
        init(context);

    }

    public EmailAutoCompleteTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context) {
        setAdapter(new EmailAutoCompleteAdapter(context, emailSuffix));
        setThreshold(1);
        setOnFocusChangeListener(this);
    }

    @Override
    protected void replaceText(CharSequence text) {
        String account = EmailAutoCompleteTextView.this.getText().toString();
        int index = account.indexOf("@");
        if (index != -1) {
            account = account.substring(0, index);
        }
        String email = account + text;
        super.replaceText(email);
    }

    @Override
    protected void performFiltering(CharSequence text, int keyCode) {
        Log.i("DebugInfo", "performFiltering: " + text.toString() + "\t" + keyCode);
        String inputContent = text.toString();
        int index = inputContent.indexOf("@");
        if (index == -1) {
            if (inputContent.matches("^[a-zA-Z0-9_]+$")) {
                super.performFiltering("@", keyCode);
            } else {
                dismissDropDown();
            }
        } else {
            super.performFiltering(inputContent.substring(index), keyCode);
        }
    }

    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        String address = EmailAutoCompleteTextView.this.getText().toString();
        if (hasFocus) {
            Log.d("DebugInfo", "得到焦点");
            if (StringUtils.isNotBlank(address)) {
                performFiltering(address, 0);
            } else {
                if (StringUtils.isBlank(address) || !address.matches("^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\\.[a-zA-Z0-9_-]+)+$")) {
                    Toast.makeText(getContext(), "邮箱地址格式不正确", Toast.LENGTH_SHORT).show();
                    setError("邮箱地址格式不正确");
                }
            }
        }
    }

    class EmailAutoCompleteAdapter extends ArrayAdapter<String> {

        public EmailAutoCompleteAdapter(@NonNull Context context, @NonNull String[] objects) {
            super(context, R.layout.dropdown_item_email_auto_complete, objects);
        }

        @NonNull
        @Override
        public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
            ViewHolder holder;
            View itemView = convertView;
            if (convertView == null) {
                itemView = LayoutInflater.from(getContext())
                        .inflate(R.layout.dropdown_item_email_auto_complete, parent, false);
                holder = new ViewHolder(itemView);
            } else {
                holder = (ViewHolder) itemView.getTag();
            }
            String emailSuffix = getItem(position);
            holder.refreshView(emailSuffix);
            return itemView;
        }

        class ViewHolder {
            public View rootView;
            public TextView text;

            public ViewHolder(View rootView) {
                rootView.setTag(this);
                this.rootView = rootView;
                this.text = (TextView) rootView.findViewById(R.id.text);
            }

            public void refreshView(String emailSuffix) {
                String account = EmailAutoCompleteTextView.this.getText().toString();
                int index = account.indexOf("@");
                if (index != -1) {
                    account = account.substring(0, index);
                }
                String email = account + emailSuffix;
                text.setText(email);
            }
        }
    }
}
